package br.com.gaacorp.sai.webapp.action;

import java.io.Serializable;

import br.com.gaacorp.sai.model.University;
import br.com.gaacorp.sai.service.UniversityManager;

public class UniversityForm extends BasePage implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 124739921814811647L;
	private UniversityManager universityManager;
	private University university = new University();
	private Long universityId;

	public String delete() {
		universityManager.remove(university.getUniversityId());
		addMessage("university.deleted");

		return "universityList";
	}

	public String edit() {
		// Comparison to zero (vs. null) is required with MyFaces 1.2.2, not
		// with previous versions
		if (universityId != null && universityId != 0) {
			university = universityManager.get(universityId);
		} else {
			university = new University();
		}

		return "universityForm";
	}

	public University getUniversity() {
		return university;
	}

	public String save() {
		boolean isNew = (university.getUniversityId() == null || university
				.getUniversityId() == 0);
		universityManager.save(university);

		String key = (isNew) ? "university.added" : "university.updated";
		addMessage(key);

		if (isNew) {
			return "universityList";
		} else {
			return "universityForm";
		}
	}

	public void setUniversity(University university) {
		this.university = university;
	}

	public void setUniversityId(Long universityId) {
		this.universityId = universityId;
	}

	public void setUniversityManager(UniversityManager universityManager) {
		this.universityManager = universityManager;
	}
}